<template>
  <div class="login">
    <div class="container">
      <div class="avatar">
        <img src="../assets/avatar.jpg" alt="" />
      </div>
      <el-input
        placeholder="请输入手机号"
        prefix-icon="el-icon-user-solid"
        v-model="user.username"
      >
      </el-input>
      <el-input
        show-password
        placeholder="请输入密码"
        prefix-icon="el-icon-key"
        v-model="user.password"
      >
      </el-input>
      <el-button @click="loginFn" class="login-btn" type="primary"
        >登录</el-button
      >
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user";
export default {
  data() {
    return {
      user: {
        username: "17666582215",
        password: "333333",
      },
    };
  },
  methods: {
    // 登录
    loginFn() {
      let reg = /^1[3|4|5|6|7|8|9]\d{9}$/;
      let regPw = /^.{3,16}$/;
      if (!reg.test(this.user.username)) {
        this.$message.error("手机号码输入错误！");
        return;
      } else if (!regPw.test(this.user.password)) {
        this.$message.error("密码输入错误！");
        return;
      }
      login(this.user).then((res) => {
        console.log(38, res);
        if (res.data.message == "用户不存在") {
          this.$message.error(res.data.message);
        } else {
          this.$message.success(res.data.message);
          // 保存token
          localStorage.setItem("hm-toutiao_token", res.data.data.token);
          // 保存用户信息
          localStorage.setItem(
            "hm-user-backend",
            JSON.stringify(res.data.data.user)
          );

          // 跳转页面，跳转到首页
          this.$router.push("/index");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;
  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .el-input {
      margin-bottom: 20px;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>